package nextel.dao;

import nextel.visita.bean.TransaccionBean;
import pe.com.nextel.rms.DataInputStreamX;
import java.io.InputStream;
import nextel.usuario.bean.AvanceBean;
import nextel.usuario.bean.UsuarioBean;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.XmlPullParser;
import pe.com.nextel.util.Iterator;
import pe.com.nextel.util.List;

/**
 *
 * @author eespinoza
 */
public class XMLDAO {

    private static final XMLDAO xml = new XMLDAO();

    public static XMLDAO getInstance() {
        return xml;
    }

    private XMLDAO() {
    }

    /*
    /**********************************/
    /* Usuarios
    /**********************************/
    //Genero el documento XML que envio al servidor para validar el ingreso
    //Parseo el xml con los datos del Usuario
    public UsuarioBean parseXMLUsuario(KXmlParser parser) throws Exception {
        UsuarioBean usuario = null;
        
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("u") && (parser.getAttributeCount() > 0)) {//si se lee un bean
                        
                        usuario = new UsuarioBean();
                        usuario.readXMLObject(parser);
                    }

                    if (parser.getName().equals("e") && (parser.getAttributeCount() > 0)) {
                        //si se lee un bean
                        throw new Exception(parser.getAttributeValue(null, "m"));
                    }
                    break;
            }
            eventType = parser.next();
        }
        return usuario;
    }
    
     public AvanceBean parseXMLAvance(KXmlParser parser) throws Exception {
        AvanceBean avance = null;
        
        int eventType = parser.getEventType();
        
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("a") && (parser.getAttributeCount() > 0)) {//si se lee un bean
                        
                        avance = new AvanceBean();
                        avance.readXMLObject(parser);
                    }

                    if (parser.getName().equals("e") && (parser.getAttributeCount() > 0)) {
                        //si se lee un bean
                        throw new Exception(parser.getAttributeValue(null, "m"));
                    }
                    break;
            }
            eventType = parser.next();
        }
        return avance;
    }


    /**********************************/
    /*  Pedido                       */
    /*********************************/
    public void parseMensaje(KXmlParser parser) throws Exception {

        while (parser.getEventType() != XmlPullParser.END_DOCUMENT) {

            parser.next();

            if (parser.getEventType() == XmlPullParser.START_TAG && parser.getName().equals("e")) {
                throw new Exception(parser.getAttributeValue(null, "m"));
            }
        }
    }

    public void parseMensaje(InputStream in) throws Exception {
        //System.out.println("parseMensaje");

        DataInputStreamX dataInputStream = null;

        try {

            dataInputStream = new DataInputStreamX(in);

            String mensaje = dataInputStream.readString();
            //System.out.println("mensaje " + mensaje);
            if (!"".equals(mensaje)) {
                throw new Exception(mensaje);
            }

        } finally {
            try {
                if (in != null) {
                    in.close();
                    in = null;
                }
                if (dataInputStream != null) {
                    dataInputStream.close();
                    dataInputStream = null;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String writeXMLServicio(List lista) throws Exception {
        StringBuffer str = new StringBuffer();
        str.append("<lp>");
        for (Iterator it = lista.iterator(); it.hasNext();) {
            TransaccionBean bean = (TransaccionBean) it.next();
            str.append(new String(bean.writeXML()));
        }
        str.append("</lp>");
        return str.toString();
    }

    public String writeXMLServicio(TransaccionBean bean) throws Exception {
       // System.out.println("ingreso writeXMLServicio");
        StringBuffer str = new StringBuffer();
        str.append("<lp>");
        str.append(new String(bean.writeXML()));
        //System.out.println("bean.writeXML()" + bean.writeXML());
        str.append("</lp>");
        return str.toString();
    }

    public void parseXMLServicio(KXmlParser parser) throws Exception {

        int eventType = parser.getEventType();


        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG && parser.getName().equals("e")) {//si se lee un usuario
                throw new Exception(parser.getAttributeValue(null, "m"));
            }


            eventType = parser.next();
        }
    }
    
    public String writeXMLAvance(AvanceBean bean) throws Exception {
       // System.out.println("ingreso writeXMLServicio");
        StringBuffer str = new StringBuffer();
        str.append("<a>");
        str.append(new String(bean.writeXML()));
        //System.out.println("bean.writeXML()" + bean.writeXML());
        str.append("</a>");
        return str.toString();
    }
}

